<?php

namespace app\index\dao;

use app\common\model\ConsultingServer;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * ConsultingServerDao 数据访问对象
 * Class MemberAskServerDao
 * @package app\dao
 * @method ConsultingServer findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method ConsultingServer insertActive(array $params, array $allow_field = [])
 * @method ConsultingServer updateActive(ConsultingServer $model, array $params, array $allow_field = [])
 * @method ConsultingServer findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method ConsultingServer updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(ConsultingServer $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class ConsultingServerDao
{
    use DaoTrait;

    /**
     * @var \app\common\model\ConsultingServer
     */
    private $baseModel;

    /**
     * MemberAskServerDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = ConsultingServer::class;
    }

    /**
     * 卡片消息用的
     * @param $id
     * @return ConsultingServer|null
     */
    public function findCardMessage($id)
    {
        return ConsultingServer::where('id', $id)
            ->withAttr([
                'deltaPrice' => fn($value, $data) => $data['showPrice'] - $data['price'],
            ])
            ->append(['iconImg', 'deltaPrice', 'questionsNumStr'])
            ->find();
    }

    /**
     * 卡片消息用的
     * @param $ids
     * @return \think\Collection
     */
    public function selectCardMessage($ids)
    {
        return ConsultingServer::whereIn('id', $ids)
            ->withAttr([
                'deltaPrice' => fn($value, $data) => $data['showPrice'] - $data['price'],
            ])
            ->append(['iconImg', 'deltaPrice', 'questionsNumStr'])
            ->select();
    }

    /**
     * 咨询内的更多服务
     * @return \think\Collection
     */
    public function selectConsultingMoreServer()
    {
        return ConsultingServer::where('serverType', 1)
            ->where('deleted', '0')
            ->append(['iconImg', 'questionsNumStr'])
            ->order('ord', 'ASC')
            ->select();
    }

    /**
     * 极速咨询
     * @param $page
     * @param $page_size
     * @return ConsultingServer[]|array|\think\Collection
     */
    public function selectQuickConsultingServer($page, $page_size)
    {
        return ConsultingServer::whereIn('serverType', [3, 4, 5])
            ->where('deleted', '0')
            ->append(['iconImg'])
            ->orderValues('serverType', [3, 5, 4])
            ->order('ord', 'ASC')
            ->paging($page, $page_size)
            ->select();
    }

    public function findConsultingServerByServerType($id, $serverType)
    {
        return ConsultingServer::when(is_id($id), fn($query) => $query->where('id', $id))
            ->when(is_numeric($serverType), fn($query) => $query->where('serverType', $serverType))
            ->where('deleted', '0')
            ->append(['iconImg', 'questionsNumStr'])
            ->order('ord', 'ASC')
            ->find();
    }

    public function selectOptions()
    {
        return ConsultingServer::where('deleted', '0')
            ->field(['id', 'name', 'serverType'])
            ->orderValues('serverType', [2, 6, 3, 4, 5, 1])
            ->order('ord', 'ASC')
            ->select();
    }
}
